#include "searchlineedit.h"

#include <QStyle>

SearchLineEdit::SearchLineEdit(QWidget *parent) :
	QLineEdit(parent),
	m_has_text(false) {
	resetSearchTooltip();
	connect(this, SIGNAL(textEdited(const QString&)), this, SLOT(updateHasTextState(const QString&)));
}

SearchLineEdit::~SearchLineEdit() {

}

void SearchLineEdit::resetSearchTooltip() {
	setText("Search");
	setStyleSheet("QLineEdit {color:gray}");
}

QString SearchLineEdit::text() const {
	if (hasText()) {
		return QLineEdit::text();
	} else {
		return QString();
	}
}

void SearchLineEdit::focusInEvent(QFocusEvent* /* event */) {
	if (!hasText()) {
		setStyleSheet("QLineEdit {color:black}");
		setText("");
	}
}

void SearchLineEdit::focusOutEvent(QFocusEvent* /* event */) {
	if (!hasText()) {
		setStyleSheet("QLineEdit {color:grey}");
		setText("Search");
	}
}

void SearchLineEdit::updateHasTextState(const QString &new_text) {
	setHasText(new_text.size() != 0);
}
